package com.drink.yan.svc.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drink.yan.core.mapper.YanActivityMapper;
import com.drink.yan.core.mapper.YanActivityUserMapper;
import com.drink.yan.core.mapper.YanSkuMapper;
import com.drink.yan.intf.define.*;
import com.drink.yan.intf.entity.*;
import com.drink.yan.intf.entity.io.YanJsonMapIo;
import com.drink.yan.intf.vo.ActivityVo;
import com.drink.yan.intf.vo.SkuVo;
import com.drink.yan.web.rest.req.cmd.ApplyActivityCmd;
import com.drink.yan.web.rest.req.cmd.ChangeSkuStatusCmd;
import com.drink.yan.web.rest.req.qry.PageCourseSkuQry;
import com.drink.yan.web.rest.req.qry.PageSkuQry;
import com.drink.yan.web.rest.req.qry.PageStudySkuQry;
import com.meta.act.app.service.ISysDictDataService;
import com.meta.act.app.service.ISysDictTypeService;
import com.meta.act.app.service.ISysUserService;
import com.meta.act.entity.SysDictData;
import com.meta.framework.act.entity.SysUser;
import com.meta.framework.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * java类简单作用描述
 *
 * @Title:
 * @Package: com.drink.yan.svc.service.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023/11/24 20:44
 * @Version: v2.0
 */
@Service
public class YanSkuService extends ServiceImpl<YanSkuMapper, YanSku> {

	@Resource
	private YanCommissionService yanCommissionService;

	@Resource
	private ISysDictTypeService iSysDictTypeService;

	@Resource
	private ISysUserService iSysUserService;


	public List<SkuVo> listCourseSku(PageCourseSkuQry qry) {

		/*QueryWrapper<YanSku> query = new QueryWrapper<>();
		LambdaQueryWrapper<YanSku> queryWrapper = query.lambda();
		queryWrapper.eq(YanSku::getStatus, YanSkuStatusE.on_shelf.getCode());
		//热门逻辑：
		if("hot".equals(qry.getQueryType())){
			queryWrapper.orderByDesc(YanSku::getOrderCount);
			queryWrapper.last("limit 2");
		}else{
			for(String category : qry.getCategorys()){
				query.la
			}
			for(String label : qry.getLabels()){
				queryWrapper.or().like(YanSku::getSkuLabels, label);
			}
		}*/
		qry.setStatus(YanSkuStatusE.on_shelf.getCode());
		List<SkuVo> skuVos = this.baseMapper.listCourseSku(qry);
		convertSkuVo(skuVos);
		return skuVos;
	}

	public List<SkuVo> listStudySku(PageStudySkuQry qry) {

		LambdaQueryWrapper<YanSku> queryWrapper = new LambdaQueryWrapper<>();
		PageSkuQry pageSkuQry = new PageSkuQry();
		pageSkuQry.setSkuName(qry.getSkuName());
		pageSkuQry.setSkuLabelsList(qry.getLabelList());
		pageSkuQry.setSkuCategoryList(qry.getCategoryList());
		pageSkuQry.setSkuType(qry.getType());
		pageSkuQry.setStatus(YanSkuStatusE.on_shelf.getCode());
		List<SkuVo> skuVos = this.baseMapper.listSku(pageSkuQry);
		convertSkuVo(skuVos);
		return skuVos;
	}

	public SkuVo getSkuDetail(Long skuId) {

		YanSku yanSku = this.getById(skuId);
		SkuVo skuVo = yanSku.transform(SkuVo.class);
		SysUser sysUser = iSysUserService.selectUserById(yanSku.getCreateBy());
		skuVo.setCreateByName(sysUser.getNickName());
		return skuVo;
	}

	public Boolean deleteSku(Long skuId) {

		this.baseMapper.deleteById(skuId);

		return true;
	}

	public YanSku saveSku(YanSku yanSku) {
		if (Objects.isNull(yanSku.getSkuId())) {

			yanSku.setBelongUid(SecurityUtils.getUserId());
			yanSku.setCreateBy(SecurityUtils.getUserId());
			yanSku.setCreateTime(LocalDateTime.now());
			if (YanSkuOriginalE.senior.getCode().equals(yanSku.getSkuOriginal())) {
				yanSku.setCheckStatus(YanSkuCheckStatusE.checking.getCode());
				yanSku.setStatus(YanSkuStatusE.wait_shelf.getCode());
			} else {
				yanSku.setCheckStatus(YanSkuCheckStatusE.check_succ.getCode());
				yanSku.setStatus(YanSkuStatusE.wait_shelf.getCode());
			}
			this.save(yanSku);
		} else {
			this.updateById(yanSku);
		}
		return yanSku;
	}

	public YanSku statusCheckSucc(ChangeSkuStatusCmd cmd) {

		YanSku skuDetail = getSkuDetail(cmd.getSkuId());
		skuDetail.setCheckStatus(YanSkuCheckStatusE.check_succ.getCode());
		saveSku(skuDetail);

		//给学姐方法佣金
		if (YanSkuOriginalE.senior.getCode().equals(skuDetail.getSkuOriginal())) {
			YanCommission yanCommission = new YanCommission();
			//yanCommission.setStudentId(cmd.getUserId());
			yanCommission.setUserId(skuDetail.getBelongUid());
			yanCommission.setCommissionType(YanCommissionTypeE.upload_ebook.getCode());
			yanCommission.setCommissionStatus(YanCommissionStatusE.in_account.getCode());
			yanCommission.setCommissionTypeName(YanCommissionTypeE.upload_ebook.getRemark());
			yanCommission.setCommissionName(skuDetail.getSkuName());
			yanCommission.setBusinessAmount(skuDetail.getSkuPrice());
			yanCommission.setBusinessKey(skuDetail.getSkuId());
			yanCommission.setBusinessSubKey(skuDetail.getSkuId());
			yanCommission.setUserType(YanUserTypeE.senior.getCode());
			yanCommissionService.generateCommission(yanCommission);
		}
		return skuDetail;
	}

	public YanSku statusCheckFail(ChangeSkuStatusCmd cmd) {

		YanSku skuDetail = getSkuDetail(cmd.getSkuId());
		skuDetail.setCheckStatus(YanSkuCheckStatusE.check_fail.getCode());
		skuDetail.setRemark(cmd.getRemark());
		saveSku(skuDetail);

		return skuDetail;
	}

	public YanSku statusOnShelf(ChangeSkuStatusCmd cmd) {
		YanSku skuDetail = getSkuDetail(cmd.getSkuId());
		skuDetail.setStatus(YanSkuStatusE.on_shelf.getCode());
		saveSku(skuDetail);

		return skuDetail;
	}

	public YanSku statusOffShelf(ChangeSkuStatusCmd cmd) {
		YanSku skuDetail = getSkuDetail(cmd.getSkuId());
		skuDetail.setStatus(YanSkuStatusE.off_shelf.getCode());
		saveSku(skuDetail);

		return skuDetail;
	}

	public List<SkuVo> listSku(PageSkuQry qry) {

		List<SkuVo> yanSkus = this.baseMapper.listSku(qry);

		convertSkuVo(yanSkus);
		return yanSkus;
	}

	private void convertSkuVo(List<SkuVo> yanSkus) {
		Map<String, String> ebook_audit_status = iSysDictTypeService.selectDictMapByType("ebook_audit_status");
		Map<String, String> ebook_goods_status = iSysDictTypeService.selectDictMapByType("ebook_goods_status");

		yanSkus.forEach(x->{
			x.setStatusName(ebook_goods_status.get(x.getStatus()));
			x.setCheckStatusName(ebook_audit_status.get(x.getCheckStatus()));
			x.setSkuImgs(JSONObject.parseArray(x.getSkuImgsStr(), String.class));
			x.setSkuLabels(JSONObject.parseArray(x.getSkuLabelsStr(), String.class));
			x.setSkuFiles(JSONObject.parseArray(x.getSkuFilesStr(), YanJsonMapIo.class));
		});
	}
}
